package com.example.tech.folio.controller;

import com.example.tech.folio.mapper.ProjectMapper;
import com.example.tech.folio.pojo.entity.Project;
import com.example.tech.folio.pojo.param.ProjectAddNewParam;
import com.example.tech.folio.pojo.param.ProjectUpdateParam;
import com.example.tech.folio.pojo.vo.ProjectVO;
import com.example.tech.folio.web.JsonResult;
import com.example.tech.folio.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/project")
public class ProjectController {
    @Autowired
    private ProjectMapper projectMapper;

    @PostMapping("/add-new")
    public JsonResult addNew(ProjectAddNewParam param) {
        Project project = new Project();
        BeanUtils.copyProperties(param, project);
        project.setIsDisplay(1);
        projectMapper.insert(project);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/delete")
    public JsonResult deleteById(@PathVariable Long id) {
        // 检查数据是否存在
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }

        // 执行删除
        projectMapper.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/toggle-display")
    public JsonResult toggleDisplay(@PathVariable Long id, Integer isDisplay) {
        // 检查数据是否存在
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }

        // 执行修改
        Project updateProject = new Project();
        updateProject.setId(id);
        updateProject.setIsDisplay(isDisplay);
        projectMapper.updateById(updateProject);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/update")
    public JsonResult updateById(@PathVariable Long id, ProjectUpdateParam param) {
        // 检查数据是否存在
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }

        // 执行修改
        BeanUtils.copyProperties(param, project);
        projectMapper.updateById(project);
        return JsonResult.ok();
    }

    @GetMapping("/{id}")
    public JsonResult selectById(@PathVariable Long id) {
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }
        return JsonResult.ok(project);
    }

    @GetMapping("/public/{id}")
    public JsonResult selectByIdPublic(@PathVariable Long id) {
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }
        if (project.getIsDisplay() == 0) {
            throw new RuntimeException(State.ERR_NOT_FOUND);
        }

        ProjectVO projectVO = new ProjectVO();
        BeanUtils.copyProperties(project, projectVO);
        return JsonResult.ok(projectVO);
    }

    @GetMapping("/list")
    public JsonResult list() {
        List<Project> projectList = projectMapper.selectAll();
        return JsonResult.ok(projectList);
    }

    @GetMapping("/public")
    public JsonResult listPublic() {
        List<Project> projectList = projectMapper.selectAll();
        List<ProjectVO> projectVOList = new ArrayList<>();
        for (Project project : projectList) {
            if (project.getIsDisplay() == 1) {
                ProjectVO projectVO = new ProjectVO();
                BeanUtils.copyProperties(project, projectVO);
                projectVOList.add(projectVO);
            }
        }
        return JsonResult.ok(projectVOList);
    }

}